/*
 * @Author: mikey.zhaopeng 
 * @Date: 2018-12-16 22:25:26 
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2018-12-18 22:45:59
 */
import 'package:http/http.dart' as http;
import 'dart:convert';

import 'package:flutter/material.dart';
import '../widgets/search_bar.dart';
import '../widgets/video_list_tail.dart' show VideoListTail;

class SearchPage extends StatefulWidget {
  @override
  _StatePageState createState() => _StatePageState();
}

class _StatePageState extends State<SearchPage> {
  List searchList;
  List _keyword;
  final url =
        "http://ios.zmzapi.com/index.php?accesskey=519f9cab85c8059d17544947k361a827&client=1&g=api/v3&m=index&a=hot_keywords";
  @override
  void initState() {
    super.initState();
    searchList = [];
    _keyword =[];
    getKeyWord();
  }
  //获取热门搜索内容
  void getKeyWord() async{
    var response = await http.get(url);
    List map = json.decode(response.body)["data"];
    setState(() {
        _keyword = map;
    });
  }
  void getSearchInfo(String keyword) async{
    final url = "http://ios.zmzapi.com/index.php?accesskey=519f9cab85c8059d17544947k361a827&client=1&g=api/v3&m=index&a=search&k=$keyword&limit=20&page=1&st=resource";
    var response = await http.get(url);
    List map = json.decode(response.body)['data']['resource'];
    setState(() {
      searchList = map;
      print(searchList.length);
    });
  }
  @override
  Widget build(BuildContext context) {
    //热门搜索wigget
    Widget HotSearch = Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                SizedBox(
                  height: 8.0,
                ),
                Text("热门搜索"),
                Divider(),
                Wrap(
                  spacing: 5.0,
                  runSpacing: 5.0,
                  children: _keyword.map((v){
                    return GestureDetector(
                      child: Chip(
                        label:Text(v["keyword"]),
                      ),
                      onTap: (){
                        getSearchInfo(v["keyword"]);
                      },
                    );
                  }).toList(),
                ),
              ],
            ),
            ),
        ],
      ),
    );

    //搜索结果widget
    Widget ResultList = Container(
      child: ListView.builder(
        shrinkWrap: true,
        physics: ClampingScrollPhysics(),
        itemCount: searchList.length,
        itemBuilder: (BuildContext context,int index){
          return GestureDetector(
            child: VideoListTail(resource:searchList[index],),
            onTap:(){
              print(searchList[index]);
              // Navigator.of(context).push(MaterialPageRoute(
              //   builder:(context)=>VideoPage(
              //     id:searchList[index]['id'],
              //     poster:searchList[index].poster,
              //     title:searchList[index].title,
              //   )
              // ));
            },
          );
        },
      ),
    );

  
    return Scaffold(
      appBar: AppBar(
        title: SearchBar(
          enabled: true,
          onSubmitted: (String keyword){
            if (keyword.isEmpty) {
              return;
            }
            getSearchInfo(keyword);
          },
        ),
      ),
      body:ListView(
        children: <Widget>[
          searchList.length ==0?HotSearch:ResultList,
        ],
      ),
    );
  }
}
